package org.example.volunteerend.mapper;

import java.time.LocalDateTime;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.example.volunteerend.entity.VolunteerProject;

@Mapper
public interface VolunteerProjectMapper {
    // 复杂条件查询，将在XML中实现
    List<VolunteerProject> findByCondition(
            @Param("keyword") String keyword,
            @Param("categoryId") Integer categoryId,
            @Param("status") String status,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime,
            @Param("location") String location,
            @Param("sortBy") String sortBy,
            @Param("sortOrder") String sortOrder
    );
    
    // 复杂条件统计，将在XML中实现
    int countByCondition(
            @Param("keyword") String keyword,
            @Param("categoryId") Integer categoryId,
            @Param("status") String status,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime,
            @Param("location") String location
    );
    
    @Select("SELECT * FROM volunteer_projects WHERE project_id = #{projectId}")
    @Results({
        @Result(property = "projectId", column = "project_id", id = true),
        @Result(property = "projectName", column = "project_name"),
        @Result(property = "startTime", column = "start_time"),
        @Result(property = "endTime", column = "end_time"),
        @Result(property = "maxVolunteers", column = "max_volunteers"),
        @Result(property = "currentVolunteers", column = "current_volunteers"),
        @Result(property = "coverImageUrl", column = "cover_image_url"),
        @Result(property = "organizerId", column = "organizer_id"),
        @Result(property = "organizer", column = "organizer_id", 
                one = @One(select = "org.example.volunteerend.mapper.UserMapper.findById")),
        @Result(property = "categories", column = "project_id", 
                many = @Many(select = "org.example.volunteerend.mapper.VolunteerProjectMapper.findCategoriesByProjectId"))
    })
    VolunteerProject findById(@Param("projectId") Integer projectId);
    
    @Select("SELECT c.* FROM project_categories c " +
           "JOIN project_category_mapping m ON c.category_id = m.category_id " +
           "WHERE m.project_id = #{projectId}")
    List<org.example.volunteerend.entity.ProjectCategory> findCategoriesByProjectId(@Param("projectId") Integer projectId);
    
    @Select("SELECT u.* FROM users u " +
           "JOIN volunteer_registrations r ON u.user_id = r.user_id " +
           "WHERE r.project_id = #{projectId} AND r.status IN ('已报名', '已确认') " +
           "LIMIT 10")
    List<org.example.volunteerend.entity.User> findVolunteersByProjectId(@Param("projectId") Integer projectId);
    
    @Insert("INSERT INTO volunteer_projects (project_name, description, start_time, end_time, " +
            "location, max_volunteers, current_volunteers, status, organizer_id) " +
            "VALUES (#{project.projectName}, #{project.description}, #{project.startTime}, #{project.endTime}, " +
            "#{project.location}, #{project.maxVolunteers}, 0, '招募中', #{organizerId})")
    @Options(useGeneratedKeys = true, keyProperty = "project.projectId")
    int insert(@Param("project") VolunteerProject project, @Param("organizerId") Integer organizerId);
    
    @Insert("INSERT INTO project_category_mapping (project_id, category_id) VALUES (#{projectId}, #{categoryId})")
    int insertCategoryMapping(@Param("projectId") Integer projectId, @Param("categoryId") Integer categoryId);
    
    @Update("UPDATE volunteer_projects SET cover_image_url = #{coverImageUrl} WHERE project_id = #{projectId}")
    int updateCoverImage(@Param("projectId") Integer projectId, @Param("coverImageUrl") String coverImageUrl);
    
    @Update("UPDATE volunteer_projects SET project_images = #{projectImages} WHERE project_id = #{projectId}")
    int updateProjectImages(@Param("projectId") Integer projectId, @Param("projectImages") String projectImages);
} 